//
//  TitleHeaderView.swift
//  MyWeiBlog
//
//  Created by laichunhui on 15/10/8.
//  Copyright © 2015年 lch. All rights reserved.
//

import UIKit

class TitleHeaderView: UIView {
    
    private var titleButtons = [NoHighlightButton]()
    /// 指示器
    private let bottomLineView: UIView = UIView()
    /// 当前选中的标题按钮
    private var selectedBtn: UIButton?
    
    weak var delegate: TitleHeaderViewDelegate?
    
    /// 便利构造方法
    convenience init(titles: NSArray) {
        self.init()
        // 设置主题
        for (index, title) in titles.enumerate()
        {
            setButton(title: title as! String, tag:index)
        }
        //默认选中第一个标题
      titleButtonClick(titleButtons[0])
        
        // 设置底部线条View
        setBottomLineView()
    }
    
    private func setBottomLineView() {
        bottomLineView.backgroundColor = UIColor.yellowColor()

        addSubview(bottomLineView)
    }
    
    private func setButton(title title: String, tag: Int) {
        let button = NoHighlightButton()
        button.setTitleColor(UIColor.yellowColor(), forState: UIControlState.Disabled)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.tag = tag
        button.titleLabel?.font = NavTitleFont
        button.addTarget(self, action: "titleButtonClick:", forControlEvents: .TouchUpInside)
        button.setTitle(title, forState: .Normal)
        
        titleButtons.append(button)
        addSubview(button)
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        bottomLineView.height = 3
        bottomLineView.y = height - bottomLineView.height
        
        let btnW = width / CGFloat(titleButtons.count)
        
        for (index, button) in titleButtons.enumerate()
        {
            button.frame = CGRectMake(CGFloat(index) * btnW, 0, btnW, height)
            
        }
    }
    
    func titleButtonClick(sender: NoHighlightButton?
        )
    {   //修改按钮状态
        selectedBtn?.enabled = true
        sender!.enabled = false
        selectedBtn = sender
        
        //指示条滚动
        layoutIfNeeded() //需要气质更新frame，否则显示不正常
        sender!.titleLabel?.sizeToFit()

        bottomViewScrollTo(sender!)
        
        //控制器页面切换
        delegate?.titleHeaderView(self, didClickBtn: sender!, forIndex: sender!.tag)
    }
    
    func bottomViewScrollTo(button: UIButton) {
 
        UIView.animateWithDuration(0.2, animations: { () -> Void in
            self.bottomLineView.centerX = button.centerX
            self.bottomLineView.width = (button.titleLabel?.width)!
        })
    }
    
    func clickBtnWithIndex(index: Int) {
        let btn: NoHighlightButton = self.titleButtons[index]
        self.titleButtonClick(btn)
    }
}

/// DoubleTextViewDelegate协议
protocol TitleHeaderViewDelegate: NSObjectProtocol{
    
    func titleHeaderView(titleHeaderView: TitleHeaderView, didClickBtn btn: UIButton, forIndex index: Int)
    
}

/// 没有高亮状态的按钮
class NoHighlightButton: UIButton {
    /// 重写setFrame方法
    override var highlighted: Bool {
        didSet{
            super.highlighted = false
        }
    }
}

